package com.warrior.superdata.service.impl;

import com.warrior.superdata.enums.DatasourceType;
import com.warrior.superdata.mysql.entity.dto.ApiDatasourceDto;
import com.warrior.superdata.mysql.entity.dto.TableFieldDto;
import com.warrior.superdata.service.QuerySqlService;
import com.warrior.superdata.exceptionhandler.ApplicationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @Description: 环境策略类
 * @ClassName: SqlServiceImpl.java
 * @date: 2022/3/7 10:51
 * @author xujian
 */
@Service
public class QuerySqlServiceImpl {

    Map<String, QuerySqlService> executeSqlServiceMap = new HashMap<>();

    @Autowired
    public QuerySqlServiceImpl(List<QuerySqlService> executeSqlServiceList) {
        executeSqlServiceList.forEach(item -> executeSqlServiceMap.put(item.getDatasourceType(), item));
    }

    public List<String> testSourceConnect(ApiDatasourceDto apiDatasourceDto) throws ApplicationException {
        return executeSqlServiceMap.get(DatasourceType.getNameByType(apiDatasourceDto.getType())).testSourceConnect(apiDatasourceDto);
    }

    public Set<String> getTables(ApiDatasourceDto apiDatasourceDto) {
        return executeSqlServiceMap.get(DatasourceType.getNameByType(apiDatasourceDto.getType())).getTables(apiDatasourceDto);
    }

    public List<TableFieldDto> getTableFields(ApiDatasourceDto apiDatasourceDto, String tableName) {
        return executeSqlServiceMap.get(DatasourceType.getNameByType(apiDatasourceDto.getType())).getTableFields(apiDatasourceDto, tableName);
    }

}
